﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace Epon.EponEl5600.ViewModel
{
    /// <summary>
    ///     Table 样式ViewModel 基类
    /// </summary>
    /// <remarks>
    ///     
    /// </remarks>
    public abstract class TableBaseViewModel<TViewModel> : IDataProcessing where TViewModel : BaseViewModel<TViewModel>
    {
        private char[] _splitSign = new[] { '_' };

        private int _tableNameIndex = 1;

        protected ObservableCollection<TViewModel> MValueList = new ObservableCollection<TViewModel>();
        /// <summary>
        ///     Dic 行名称，T 对象
        /// </summary>
        /// <remarks>
        ///     
        /// </remarks>
        protected Dictionary<string, TViewModel> MValueDic = new Dictionary<string, TViewModel>();

        public ObservableCollection<TViewModel> ValueList
        {
            get { return MValueList; }
        }

        public Dictionary<string, TViewModel> ValueDic
        {
            get { return MValueDic; }
        }

        /// <summary>
        ///     分割标识 默认 '_'
        /// </summary>
        /// <value>
        ///     <para>
        ///         
        ///     </para>
        /// </value>
        /// <remarks>
        ///     
        /// </remarks>
        public char[] SplitSign
        {
            private get { return _splitSign; }
            set
            {
                if (_splitSign != value && value != null)
                    _splitSign = value;
            }
        }

        /// <summary>
        ///     TableName 获取位置
        /// </summary>
        /// <value>
        ///     <para>
        ///         
        ///     </para>
        /// </value>
        /// <remarks>
        ///     
        /// </remarks>
        public int TableNameIndex
        {
            private get { return _tableNameIndex; }
            set
            {
                if (_tableNameIndex != value)
                    _tableNameIndex = value;
            }
        }

        public virtual void Init()
        {
            MValueList.Clear();
            MValueDic.Clear();
        }

        /// <summary>
        ///     Table 行数据处理
        /// </summary>
        public virtual void UpDateValue(KeyValuePair<string, string> kvp)
        {
            string tableName;
            GetTableName(kvp.Key, SplitSign, out tableName);

            TViewModel table;
            if (!MValueDic.TryGetValue(tableName, out table))
            {
                table = (TViewModel)Activator.CreateInstance(typeof(TViewModel), tableName);
                MValueDic[tableName] = table;
                MValueList.Add(table);
            }
            table.SetPropertyValue(kvp.Key, SplitSign, kvp.Value);
        }

        public virtual void GetTableName(string key, char[] splitSign, out string tableName)
        {
            string[] strs = key.Split(SplitSign);
            tableName = strs.Length < TableNameIndex ? "" : strs[TableNameIndex];
        }

        public abstract string ProcessingSign { get; }
    }
}
